package com.len.controller;

import com.len.service.OSSService;
import com.len.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

@Controller
@RequestMapping(value = "/shop/file")
@Slf4j
public class UploadController {

    @Resource
    private OSSService ossService;

    /**
     * 单文件上传
     */
    @PostMapping(value = "upload")
    @ResponseBody
    public JsonUtil singleImgUpload(@RequestParam("file") MultipartFile file) {

        String fileName;
        try {
            if (file.isEmpty()) {
                return JsonUtil.error("文件为空");
            }
            fileName=ossService.upload(file);
            return JsonUtil.success(fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return JsonUtil.error("上传失败");
        }
    }

}
